// Stata 16

log using "temp_log", replace
insheet using "untie.csv", clear

// Clean data
drop if waiver == 2
drop if yob < 9
drop if citizen != 1

// Independent Variable: hand
// hand = 1: Control
// hand = 2: Control + Opponent Counterthreat
// hand = 3: Control + Opponent Reassurance
// hand = 4: Control + Opponent Normative-Frame
// hand = 5: Control + UN Treatment
// hand = 6: Control + Experts Treatment

// Generate 7-pt approval rate (1 = Lowest, 7 = Highest)
gen untie = .
replace untie = untie1d if untie1 == 2
replace untie = untie1n if untie1 == 3
replace untie = untie1a if untie1 == 1

// Generate proportion for untie (1 if untie > 4 [approve])
gen untie_prop = .
replace untie_prop = 1 if untie == 5  | untie == 6 | untie == 7
replace untie_prop = 0 if untie == 1  | untie == 2 | untie == 3 | untie == 4
tab untie_prop hand, column nofreq

// Generate 7-pt reputation score (1 = Damaged a Lot, 7 = Improved a Lot)
gen reputation = .
replace reputation = damage1b if damage1==2
replace reputation = 4 if damage1==3
replace reputation = damage1a if damage1==1

// Tests of proportions
foreach i of num 2/6 {
	prtest untie_prop      if hand == 1 | hand == `i', by(hand)
}

// T-tests using raw score
foreach i of num 2/6 {
	ttest reputation       if hand == 1 | hand == `i', by(hand)
}

// Robustness check

// Age
gen age = yob + 9

// Race
gen iswhite = .
replace iswhite = 1 if race == 1
replace iswhite = 0 if race != 1
replace iswhite = . if race == .

// Partisanship
gen repub = .
replace repub = 1 if pid1 == 2 | ((pid1 == 3 | pid1 == 4) & pid2n == 1)
replace repub = 0 if pid1 == 1 | ((pid1 == 3 | pid1 == 4) & pid2n != 1)
replace repub = . if pid1 == .

// Gender
gen isfemale = .
replace isfemale = 1 if gender == 2
replace isfemale = 0 if gender == 1

// Treatment dummies
gen t2 = 0
replace t2 = 1 if hand == 2
replace t2 = . if hand == .

gen t3 = 0
replace t3 = 1 if hand == 3
replace t3 = . if hand == .

gen t4 = 0
replace t4 = 1 if hand == 4
replace t4 = . if hand == .

gen t5 = 0
replace t5 = 1 if hand == 5
replace t5 = . if hand == .

gen t6 = 0
replace t6 = 1 if hand == 6
replace t6 = . if hand == .

// Appendix 3, Table A1 
olog untie t2 t3 t4 t5 t6
olog untie t2 t3 t4 t5 t6 isfemale
olog untie t2 t3 t4 t5 t6 isfemale age
olog untie t2 t3 t4 t5 t6 isfemale age iswhite
olog untie t2 t3 t4 t5 t6 isfemale age iswhite repub
olog untie t2 t3 t4 t5 t6 isfemale age iswhite repub edu
olog untie t2 t3 t4 t5 t6 isfemale age iswhite repub edu income

// Appendix 3, Table A2 
olog reputation t2 t3 t4 t5 t6
olog reputation t2 t3 t4 t5 t6 isfemale
olog reputation t2 t3 t4 t5 t6 isfemale age
olog reputation t2 t3 t4 t5 t6 isfemale age iswhite
olog reputation t2 t3 t4 t5 t6 isfemale age iswhite repub
olog reputation t2 t3 t4 t5 t6 isfemale age iswhite repub edu
olog reputation t2 t3 t4 t5 t6 isfemale age iswhite repub edu income

log close
translate "temp_log.smcl" "Output.pdf", replace
rm temp_log.smcl
export delimited untie_cleaned.csv, replace